package com.example.index.service.impl;

import com.example.index.entity.IndexImage;
import com.example.index.dao.IndexImageDao;
import com.example.index.service.IndexImageService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 首页图片（表）(IndexImage)表服务实现类
 *
 * @author makejava
 * @since 2023-08-30 17:55:56
 */
@Service("indexImageService")
public class IndexImageServiceImpl implements IndexImageService {
    @Resource
    private IndexImageDao indexImageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param imageId 主键
     * @return 实例对象
     */
    @Override
    public IndexImage queryById(Integer imageId) {
        return this.indexImageDao.queryById(imageId);
    }

    /**
     * 分页查询
     *
     * @param indexImage 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<IndexImage> queryByPage(IndexImage indexImage, PageRequest pageRequest) {
        long total = this.indexImageDao.count(indexImage);
        return new PageImpl<>(this.indexImageDao.queryAllByLimit(indexImage, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param indexImage 实例对象
     * @return 实例对象
     */
    @Override
    public IndexImage insert(IndexImage indexImage) {
        this.indexImageDao.insert(indexImage);
        return indexImage;
    }

    /**
     * 修改数据
     *
     * @param indexImage 实例对象
     * @return 实例对象
     */
    @Override
    public IndexImage update(IndexImage indexImage) {
        this.indexImageDao.update(indexImage);
        return this.queryById(indexImage.getImageId());
    }

    /**
     * 通过主键删除数据
     *
     * @param imageId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer imageId) {
        return this.indexImageDao.deleteById(imageId) > 0;
    }
}
